package com.bank.RabbitMQ;

import com.alibaba.fastjson.JSON;
import com.bank.entity.Member;
import com.bank.entity.Seckill;
import com.bank.entity.SeckillLog;
import com.bank.entity.vo.SeckillMessage;
import com.bank.entity.constant.ResponseEnum;
import com.bank.entity.vo.Response;
import com.bank.seckill.service.impl.GoodsServiceImpl;
import com.bank.seckill.service.impl.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/2/1 星期二 21:45
 */

@Service
@Slf4j
public class Consumer {

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderServiceImpl orderService;

    @RabbitListener(queues = "SeckillQueue")
    public void receiveMsg(String msg){
        log.info("接收到消息"+msg);
        SeckillMessage message = JSON.parseObject(msg, SeckillMessage.class);
        Member member = message.getMember();
        String secid = message.getSecid();
        Seckill goods = goodsService.SelcetGoodsById(secid);
        //判断库存
        if (goods.getQuantity()<1){
            return;
        }
        //判断是否重复下单




        SeckillLog order = (SeckillLog) redisTemplate.opsForValue().get("order" + member.getId() + ":" + secid);
        //SeckillLog order = (SeckillLog) redisTemplate.opsForValue().get("order" + ":" + secid);




        if (order != null) {
            return ;
        }
        orderService.seckill(member, goods);

    }

}
